{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-09-20T13:33:09.684060Z",
     "start_time": "2024-09-20T13:30:47.201347Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score, roc_auc_score\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "all_data = pd.read_csv('C:\\\\Users\\\\20937\\\\Desktop\\\\阿里比赛\\\\data_format1\\\\all_k.csv')\n",
    "\n",
    "all_data_1 = all_data\n",
    "\n",
    "all_data_1 = all_data_1.fillna(0)\n",
    "label = all_data_1['label']\n",
    "# 分离特征和标签\n",
    "features = all_data_1.drop('label', axis=1)\n",
    "\n",
    "# 数据标准化（可选项，根据数据特点决定是否需要）\n",
    "scaler = StandardScaler()\n",
    "standardized_features = scaler.fit_transform(features)\n",
    "# 将标准化后的特征转换回 DataFrame 并保留列名\n",
    "features = pd.DataFrame(standardized_features, columns=features.columns)\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(features, label, test_size=0.2, random_state=9)\n",
    "# X_test\n",
    "\n",
    "# 定义参数网格\n",
    "param_grid = {\n",
    "    'n_estimators': [50],\n",
    "    'learning_rate': [0.5],\n",
    "    'estimator__max_depth': [1]  # 修改参数名以匹配新规则\n",
    "}\n",
    "\n",
    "# 创建决策树分类器实例，将作为 AdaBoost 的基础分类器\n",
    "base_estimator = DecisionTreeClassifier(class_weight='balanced')\n",
    "\n",
    "# 创建 AdaBoost 分类器，通过 estimator 参数指定基础分类器\n",
    "ada_clf = AdaBoostClassifier(estimator=base_estimator)\n",
    "\n",
    "# 创建网格搜索对象\n",
    "grid_search = GridSearchCV(ada_clf, param_grid, cv=5, scoring='accuracy')\n",
    "\n",
    "# 将网格搜索应用于数据\n",
    "grid_search.fit(X_train, y_train)\n",
    "\n",
    "# 打印最佳参数和最佳得分\n",
    "print(\"最佳参数:\", grid_search.best_params_)\n",
    "print(\"最佳得分:\", grid_search.best_score_)\n",
    "\n",
    "# 使用最佳模型对测试数据进行预测\n",
    "y_pred = grid_search.predict(X_test)\n",
    "\n",
    "# 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(\"准确率:\", accuracy)\n",
    "\n",
    "# 如果是二分类问题，计算 AUC\n",
    "if len(set(y_test)) == 2:\n",
    "    auc = roc_auc_score(y_test, y_pred)\n",
    "    print(\"AUC:\", auc)\n",
    "\n",
    "# 输出决策树的模型参数\n",
    "best_ada_clf = grid_search.best_estimator_\n",
    "best_decision_tree = best_ada_clf.estimator_\n",
    "print(\"决策树模型参数:\", best_decision_tree.get_params())\n",
    "# \n",
    "# # 获取预测概率\n",
    "# probabilities = best_ada_clf.predict_proba(X_test)[:, 1]\n",
    "# \n",
    "# # 获取测试集在原始数据中的索引\n",
    "# test_indices =  X_test.index  # 假设 X_test 是一个可获取索引的对象，如果不是，需要根据具体情况调整\n",
    "# \n",
    "# # 创建包含预测结果的 DataFrame\n",
    "# result_df = pd.DataFrame({\n",
    "#     'user_id': all_data_1.iloc[test_indices]['user_id'],\n",
    "#     'merchant_id': all_data_1.iloc[test_indices]['merchant_id'],\n",
    "#     'prob': probabilities\n",
    "# })\n",
    "# \n",
    "# \n",
    "# # 将结果保存为 CSV 文件\n",
    "# result_df.to_csv('C:\\\\Users\\\\20937\\\\Desktop\\\\阿里比赛\\\\data_format1\\\\prediction_results.csv',index=False)\n"
   ],
   "id": "bc829f2824693bc7",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\20937\\AppData\\Roaming\\Python\\Python312\\site-packages\\sklearn\\ensemble\\_weight_boosting.py:527: FutureWarning: The SAMME.R algorithm (the default) is deprecated and will be removed in 1.6. Use the SAMME algorithm to circumvent this warning.\n",
      "  warnings.warn(\n",
      "C:\\Users\\20937\\AppData\\Roaming\\Python\\Python312\\site-packages\\sklearn\\ensemble\\_weight_boosting.py:527: FutureWarning: The SAMME.R algorithm (the default) is deprecated and will be removed in 1.6. Use the SAMME algorithm to circumvent this warning.\n",
      "  warnings.warn(\n",
      "C:\\Users\\20937\\AppData\\Roaming\\Python\\Python312\\site-packages\\sklearn\\ensemble\\_weight_boosting.py:527: FutureWarning: The SAMME.R algorithm (the default) is deprecated and will be removed in 1.6. Use the SAMME algorithm to circumvent this warning.\n",
      "  warnings.warn(\n",
      "C:\\Users\\20937\\AppData\\Roaming\\Python\\Python312\\site-packages\\sklearn\\ensemble\\_weight_boosting.py:527: FutureWarning: The SAMME.R algorithm (the default) is deprecated and will be removed in 1.6. Use the SAMME algorithm to circumvent this warning.\n",
      "  warnings.warn(\n",
      "C:\\Users\\20937\\AppData\\Roaming\\Python\\Python312\\site-packages\\sklearn\\ensemble\\_weight_boosting.py:527: FutureWarning: The SAMME.R algorithm (the default) is deprecated and will be removed in 1.6. Use the SAMME algorithm to circumvent this warning.\n",
      "  warnings.warn(\n",
      "C:\\Users\\20937\\AppData\\Roaming\\Python\\Python312\\site-packages\\sklearn\\ensemble\\_weight_boosting.py:527: FutureWarning: The SAMME.R algorithm (the default) is deprecated and will be removed in 1.6. Use the SAMME algorithm to circumvent this warning.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最佳参数: {'estimator__max_depth': 1, 'learning_rate': 0.5, 'n_estimators': 50}\n",
      "最佳得分: 0.5324091349158934\n",
      "准确率: 0.5307127454979894\n",
      "AUC: 0.7576338440991051\n",
      "决策树模型参数: {'ccp_alpha': 0.0, 'class_weight': 'balanced', 'criterion': 'gini', 'max_depth': 1, 'max_features': None, 'max_leaf_nodes': None, 'min_impurity_decrease': 0.0, 'min_samples_leaf': 1, 'min_samples_split': 2, 'min_weight_fraction_leaf': 0.0, 'monotonic_cst': None, 'random_state': None, 'splitter': 'best'}\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "import pandas as pd\n",
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score, roc_auc_score\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "all_data = pd.read_csv('C:\\\\Users\\\\20937\\\\Desktop\\\\阿里比赛\\\\data_format1\\\\all_k.csv')\n",
    "\n",
    "all_data_1 = all_data\n",
    "\n",
    "all_data_1 = all_data_1.fillna(0)\n",
    "label = all_data_1['label']\n",
    "# 分离特征和标签\n",
    "features = all_data_1.drop('label', axis=1)\n",
    "\n",
    "# 数据标准化（可选项，根据数据特点决定是否需要）\n",
    "scaler = StandardScaler()\n",
    "standardized_features = scaler.fit_transform(features)\n",
    "# 将标准化后的特征转换回 DataFrame 并保留列名\n",
    "features = pd.DataFrame(standardized_features, columns=features.columns)\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(features, label, test_size=0.2, random_state=9)\n",
    "# X_test\n",
    "\n",
    "# 定义参数网格\n",
    "param_grid = {\n",
    "    'n_estimators': [50],\n",
    "    'learning_rate': [0.5],\n",
    "    'estimator__max_depth': [1]  # 修改参数名以匹配新规则\n",
    "}\n",
    "\n",
    "# 创建决策树分类器实例，将作为 AdaBoost 的基础分类器\n",
    "base_estimator = DecisionTreeClassifier(class_weight='balanced')\n",
    "\n",
    "# 创建 AdaBoost 分类器，通过 estimator 参数指定基础分类器\n",
    "ada_clf = AdaBoostClassifier(estimator=base_estimator)\n",
    "\n",
    "# 创建网格搜索对象\n",
    "grid_search = GridSearchCV(ada_clf, param_grid, cv=5, scoring='accuracy')\n",
    "\n",
    "# 将网格搜索应用于数据\n",
    "grid_search.fit(X_train, y_train)\n",
    "\n",
    "# 打印最佳参数和最佳得分\n",
    "print(\"最佳参数:\", grid_search.best_params_)\n",
    "print(\"最佳得分:\", grid_search.best_score_)\n",
    "\n",
    "# 使用最佳模型对测试数据进行预测\n",
    "y_pred = grid_search.predict(X_test)\n",
    "\n",
    "# 计算准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(\"准确率:\", accuracy)\n",
    "\n",
    "# 如果是二分类问题，计算 AUC\n",
    "if len(set(y_test)) == 2:\n",
    "    auc = roc_auc_score(y_test, y_pred)\n",
    "    print(\"AUC:\", auc)\n",
    "\n",
    "# 输出决策树的模型参数\n",
    "best_ada_clf = grid_search.best_estimator_\n",
    "best_decision_tree = best_ada_clf.estimator_\n",
    "print(\"决策树模型参数:\", best_decision_tree.get_params())"
   ],
   "id": "8c24b6a187299e4b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
